define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/studentmanager/index',
                    add_url: 'user/studentmanager/add',
                    edit_url: 'user/studentmanager/edit',
                    del_url: 'user/studentmanager/del',
                    multi_url: 'user/studentmanager/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {field: 'state', checkbox: true},
                        {field: 'id', title: 'ID', operate: false},
                        {field: 'username', title: '用户名', operate: 'LIKE'},
                        {field: 'nickname', title: '昵称', operate: 'LIKE'},
                        {field: 'email', title: '邮箱', operate: 'LIKE'},
                        {field: 'mobile', title: '手机号', operate: 'LIKE'},
                        {field: 'createtime', title: '创建时间', operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'status', title: '状态', operate: false, formatter: Table.api.formatter.status},
                        {field: 'operate', title: '操作', table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                queryParams: function (params) {
                    // 添加group_id参数到查询条件，确保在POST和GET请求中都能正确传递
                    // 修改为从Config中获取groupId值
                    var groupId = Config.groupId || '';
                    if (groupId !== undefined && groupId !== '' && groupId != 0) {
                        params.group_id = groupId;
                    }
                    return params;
                }
            });
            
            // 添加用户组筛选下拉框
            $('#toolbar').prepend('<select class="form-control" id="group_id" style="width: 150px; display: inline-block; margin-right: 5px;">' +
                '<option value="">全部用户组</option>' +
                '<option value="1">普通用户</option>' +
                '<option value="2">学员</option>' +
                '<option value="3">教师</option>' +
                '</select>');
            
            // 绑定用户组筛选事件
            $('#group_id').on('change', function () {
                table.bootstrapTable('refresh');
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            var script = document.createElement('script');
            script.src = '/assets/js/backend/common/add.js?';
            document.head.appendChild(script);
            Controller.api.bindevent();
        },
        edit: function () {
            var script = document.createElement('script');
            script.src = '/assets/js/backend/common/add.js?';
            document.head.appendChild(script);
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});